package com.nanjia.boot.batch_stock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.batch_stock.model.form.WarehouseBatchStockForm;
import com.nanjia.boot.batch_stock.model.query.WarehouseBatchStockQuery;
import com.nanjia.boot.batch_stock.model.vo.WarehouseBatchStockVO;
import com.nanjia.boot.batch_stock.service.WarehouseBatchStockService;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.manufacturer.model.query.ManufacturerQuery;
import com.nanjia.boot.manufacturer.model.vo.ManufacturerVO;
import com.nanjia.boot.manufacturer.service.ManufacturerService;
import com.nanjia.boot.product.sku.model.query.ProductSkuQuery;
import com.nanjia.boot.product.sku.model.vo.ProductSkuVO;
import com.nanjia.boot.product.sku.service.ProductSkuService;
import com.nanjia.boot.product.spu.model.query.ProductSpuQuery;
import com.nanjia.boot.product.spu.model.vo.ProductSpuVO;
import com.nanjia.boot.product.spu.service.ProductSpuService;
import com.nanjia.boot.stock.productbatch.model.query.ProductBatchQuery;
import com.nanjia.boot.stock.productbatch.model.vo.ProductBatchVO;
import com.nanjia.boot.stock.productbatch.service.ProductBatchService;
import com.nanjia.boot.warehouse.model.query.WarehouseQuery;
import com.nanjia.boot.warehouse.model.vo.WarehouseVO;
import com.nanjia.boot.warehouse.service.WarehouseService;
import com.nanjia.boot.warehouse.storagelocation.model.query.WarehouseStorageLocationQuery;
import com.nanjia.boot.warehouse.storagelocation.model.vo.WarehouseStorageLocationVO;
import com.nanjia.boot.warehouse.storagelocation.service.WarehouseStorageLocationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存记录前端控制层
 *
 * @author 周洋
 * @since 2025-08-22 11:27
 */
@Tag(name = "库存记录接口")
@RestController
@RequestMapping("/api/v1/warehouse-batch-stock")
@RequiredArgsConstructor
public class WarehouseBatchStockController {

    private final WarehouseBatchStockService warehouseBatchStockService;

    private final WarehouseService warehouseService;
    private final WarehouseStorageLocationService warehouseStorageLocationService;
    private final ManufacturerService manufacturerService;

    private final ProductSpuService productSpuService;

    private final ProductSkuService productSkuService;

    private final ProductBatchService productBatchService;

    @Operation(summary = "批次列表")
    @GetMapping("/getProductBatchOptions")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:query')")
    public Result<List<ProductBatchVO>> getProductBatchOptions(ProductBatchQuery queryParams) {
        List<ProductBatchVO> productBatchVOS = productBatchService.getProductBatchOptions(queryParams);
        return Result.success(productBatchVOS);
    }

    @Operation(summary = "仓库列表")
    @GetMapping("/getWarehouseOptions")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:query')")
    public Result<List<WarehouseVO>> getWarehouseOptions(WarehouseQuery queryParams) {
        List<WarehouseVO> warehouseVOS = warehouseService.listWarehouseVo(queryParams);
        return Result.success(warehouseVOS);
    }

    @Operation(summary = "库位列表")
    @GetMapping("/getWarehouseStorageLocationOptions")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:query')")
    public Result<List<WarehouseStorageLocationVO>> getWarehouseStorageLocationOptions(WarehouseStorageLocationQuery queryParams) {
        Result<List<WarehouseStorageLocationVO>> result = warehouseStorageLocationService.getWarehouseStorageLocationOptions(queryParams);
        return result;
    }

    @Operation(summary = "厂家列表")
    @GetMapping("/getManufacturerOptions")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:query')")
    public Result<List<ManufacturerVO>> getManufacturerOptions(ManufacturerQuery queryParams) {
        Result<List<ManufacturerVO>> result = manufacturerService.getManufacturerList(queryParams);
        return result;
    }

//    @Operation(summary = "SPU列表")
//    @GetMapping("/getSpuOptions")
//    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:query')")
//    public Result<List<ProductSpuVO>> getSpuOptions(ProductSpuQuery queryParams) {
//        List<ProductSpuVO> productSpuVOList = productSpuService.listSpu(queryParams);
//        return Result.success(productSpuVOList);
//    }

//    @Operation(summary = "SKU列表")
//    @GetMapping("/getSkuOptions")
//    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:query')")
//    public Result<List<ProductSkuVO>> getSkuOptions(ProductSkuQuery queryParams) {
//        Result<List<ProductSkuVO>> result = productSkuService.getSkuOptions(queryParams);
//        return result;
//    }

    @Operation(summary = "SKU列表")
    @GetMapping("/getList")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:query')")
    public Result<List<WarehouseBatchStockVO>> getList(WarehouseBatchStockQuery queryParams) {
        Result<List<WarehouseBatchStockVO>> result = warehouseBatchStockService.getList(queryParams);
        return result;
    }

    @Operation(summary = "库存记录分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:query')")
    public PageResult<WarehouseBatchStockVO> getWarehouseBatchStockPage(WarehouseBatchStockQuery queryParams) {
        IPage<WarehouseBatchStockVO> result = warehouseBatchStockService.getWarehouseBatchStockPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增库存记录")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:add')")
    public Result<Void> saveWarehouseBatchStock(@RequestBody @Valid WarehouseBatchStockForm formData) {
        boolean result = warehouseBatchStockService.saveWarehouseBatchStock(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取库存记录表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:edit')")
    public Result<WarehouseBatchStockForm> getWarehouseBatchStockForm(
            @Parameter(description = "库存记录ID") @PathVariable Long id
    ) {
        WarehouseBatchStockForm formData = warehouseBatchStockService.getWarehouseBatchStockFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改库存记录")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:edit')")
    public Result<Void> updateWarehouseBatchStock(
            @Parameter(description = "库存记录ID") @PathVariable Long id,
            @RequestBody @Validated WarehouseBatchStockForm formData
    ) {
        boolean result = warehouseBatchStockService.updateWarehouseBatchStock(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除库存记录")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('batch_stock:warehouse-batch-stock:delete')")
    public Result<Void> deleteWarehouseBatchStocks(
            @Parameter(description = "库存记录ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = warehouseBatchStockService.deleteWarehouseBatchStocks(ids);
        return Result.judge(result);
    }
}
